package com.sinosoft.cnmuseum.servlets;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.log4j.Logger;

import com.sinosoft.cnmuseum.dao.MbXtCzlxDao;
import com.sinosoft.cnmuseum.dao.MbXtglLogDao;
import com.sinosoft.cnmuseum.dao.MbXtglYhxxDao;
import com.sinosoft.cnmuseum.entity.MbXtglLog;
import com.sinosoft.cnmuseum.utils.PageUtil;
import com.sinosoft.cnmuseum.utils.Status;
import com.sinosoft.cnmuseum.utils.StringUtils;
import com.sinosoft.commontools.annotated.rquestMethod;
import com.sinosoft.commontools.annotated.rquestMethod.ReqMode;
import com.sinosoft.commontools.record.Record;

public class MbXtglLogServlet {
	private static Logger logger = Logger.getLogger(TestServlet.class);
	/**
	 * 日志查询
	 * @param request
	 * @param response
	 * @param data
	 * @return
	 */
	@rquestMethod(MethodNote = ReqMode.All)
	public Record getData(HttpServletRequest request, HttpServletResponse response, Record data){
		Record result = new Record();
		try{
//			MbXtglLogDao dao = new MbXtglLogDao();
//			String countsql = "select count(zdbh) as count from "+MbXtglLog.MB_XTGL_LOG_TABLE_NAME+" where 1=1 ";
//			String sql = "select * from "+MbXtglLog.MB_XTGL_LOG_TABLE_NAME+" where 1=1 ";
			
			
			MbXtglLogDao dao = new MbXtglLogDao();
			String countsql = "select count(zdbh) as count from mb_xtgl_loglist where 1=1 ";
			String sql = "select * from mb_xtgl_loglist where 1=1 ";
			
			String ordersql = " order by zdbh desc ";
			String limitsql = " limit ";
									
			int pagenumber = data.getIntValue(Status.PAGENUMBER);// 获取当前页
			int pagesize = data.getIntValue(Status.PAGESIZE);// 获取每页多少条数据
						
			String czlx = data.getStrValue(MbXtglLog.CZLX);
			String czmk = data.getStrValue(MbXtglLog.CZMK);
			String czry = data.getStrValue(MbXtglLog.CZRY);
			String nrms = data.getStrValue(MbXtglLog.NRMS);
			String kssj = data.getStrValue(MbXtglLog.KSSJ);
			String jssj = data.getStrValue(MbXtglLog.JSSJ);
			if(StringUtils.isNoEmpty(czlx)){
				countsql += " and "+MbXtglLog.CZLX+" = '"+czlx+"' ";
				sql += " and "+MbXtglLog.CZLX+" = '"+czlx+"' ";
			}
			if(StringUtils.isNoEmpty(czmk)){
				countsql += " and "+MbXtglLog.CZMK+" = '"+czmk+"' ";
				sql += " and "+MbXtglLog.CZMK+" = '"+czmk+"' ";
			}
			if(StringUtils.isNoEmpty(nrms)){
				countsql += " and "+MbXtglLog.NRMS+" like '%"+nrms+"%' ";
				sql += " and "+MbXtglLog.NRMS+" like '%"+nrms+"%' ";
			}
			if(StringUtils.isNoEmpty(czry)){
				countsql += " and czryxm like '%"+czry+"%' ";
				sql += " and czryxm like '%"+czry+"%' ";
			}	
			if(StringUtils.isNoEmpty(kssj)){
				countsql += " and "+MbXtglLog.CZSJ+" > '"+kssj+ " 00:00:00'";
				sql += " and "+MbXtglLog.CZSJ+" > '"+kssj+ " 00:00:00'";
			}
			if(StringUtils.isNoEmpty(jssj)){
				countsql += " and "+MbXtglLog.CZSJ+" < '"+jssj + " 23:59:59'";
				sql += " and "+MbXtglLog.CZSJ+" < '"+jssj + " 23:59:59'";
			}
			

			List<Record> countlist = dao.getBySql(countsql);
			int total = countlist.get(0).getIntValue("count");
			PageUtil page = new PageUtil(pagenumber, pagesize, total);
			limitsql += page.getStart()+","+pagesize;
			List<Record> list = dao.getBySql(sql+ordersql+limitsql);	
//			List<Record> mbXtglLogList = dao.getBySql(sql+ordersql+limitsql);		
//			List<Record> list = new ArrayList<Record>();
//			if(mbXtglLogList!=null && mbXtglLogList.size()>0) {
//				for (Record mbXtglLog : mbXtglLogList) {
//					int czlxid = mbXtglLog.getIntValue("czlx");
//					MbXtCzlxDao mbXtCzlxDao= new MbXtCzlxDao();
//					Record mbXtCzlx = mbXtCzlxDao.getById(czlxid);
//					String czlxname = mbXtCzlx.getStrValue("name");
//					mbXtglLog.add("czlxname", czlxname);
//					int czryid = mbXtglLog.getIntValue("czry");
//					MbXtglYhxxDao mbXtglYhxxDao =new MbXtglYhxxDao();
//					Record mbXtglYhxx = mbXtglYhxxDao.getById(czryid);
//					String czryxm = mbXtglYhxx.getStrValue("xm");
//					mbXtglLog.add("czryxm", czryxm);
//					list.add(mbXtglLog);
//				}
//			}

									
			// 组装数据
			result.add(Status.ROWS, list);
			result.add(Status.TOTAL, total);
			result.add(Status.PAGENUMBER, pagenumber);
			result.add(Status.PAGESIZE, pagesize);
			result.add(Status.PAGETOTAL, page.getPagetotal());
			// 返回接口调用成功标识
			result.add(Status.SATUS,Status.OK);
		} catch (Exception e) {
			logger.error(e.getMessage());
			result.add(Status.SATUS,Status.FAILED);
			// 返回接口调用失败信息，需要自己添加
			result.add(Status.MSG,"查询失败！");
		}
		return result;
	}

	

}
